class MyCounter2 {
    public int a;

    public  int count = 0;
    //可重入锁
     synchronized public void add() {
       synchronized (this) {
           count++;
       }
    }
    public int getCount (){
         return count;
    }
}
public class Test8 {
    //在方法内加锁


    public static void main(String[] args) throws InterruptedException {
        MyCounter2 counter2 = new MyCounter2();
        Thread t1 = new Thread(()->{
                for (int i = 0; i < 50000; i++) {
                    synchronized (counter2) {
                        counter2.add();
                    }
                }
        });
        Thread t2 = new Thread(()->{
                for (int i = 0; i < 50000; i++) {
                    counter2.add();
                }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter2.getCount());
    }
}
